/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pos.ui.jsf.converter;

import com.pos.domain.Customer;
import com.pos.ui.beans.CustomerBean;
import com.pos.ui.jsf.util.JsfUtil;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author suraj
 */
@FacesConverter(forClass = Customer.class)
public class CustomerConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        CustomerBean bean = (CustomerBean) JsfUtil.resolveExpression(context, "#{customerBean}");
        return bean.findCustomer(value);
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return "";
        }
        return String.valueOf(((Customer) value).getId());
    }
}
